﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Utils.SoftReferences;

namespace WindowsGame.Ui
{
    class UiModel
    {
        public List<UiHex> UiHexes { get; set; }
        public List<UiUnit> UiUnits { get; set; }
        public List<UiPoint> UiPoints { get; set; }

        /// <summary>
        /// The hexagon, if any, that the mouse is currently hovering over
        /// </summary>
        public IList<UiHex> HighlightedHexes { get; set; }

        public UiUnitSelection UnitSelection { get; set; }

        public SoftRefSet SoftRefSet { get; set; }

        public UiModel()
        {
            SoftRefSet = new SoftRefSet();
            UiHexes = new List<UiHex>();
            UiUnits = new List<UiUnit>();
            UiPoints = new List<UiPoint>();
            HighlightedHexes = new List<UiHex>();
        }

        public void RebuildHexUnitRelationships()
        {
            foreach (var uiHex in UiHexes)
            {
                uiHex.UiUnits = new List<UiUnit>(UiUnits.Where(uiUnit => uiUnit.Unit.Target.Location == uiHex.Hex.Target));

                foreach (var uiUnit in uiHex.UiUnits)
                {
                    uiUnit.UiHex = uiHex;
                }

                uiHex.UiPoints = UiPoints.Where(o => o.Structure.Target.Hex == uiHex.Hex.Target).ToList();
                
                foreach (var up in uiHex.UiPoints)
                {
                    up.UiHex = uiHex;
                }
            }
        }
    }
}
